/* Permute: Find all permutations of a string. The idea is to fix the 1st
 * character and recursively find the other permutations. The 1st character
 * must cycle with all the characters
 * 
 * Author: Nithin Nayak Sujir
 * Email: <firstname><lastname> at gmail dot com
 */
#include <stdio.h>
#include <string.h>

void swap(char *x, char *y)
{
    char tmp = *x;
    *x = *y;
    *y = tmp;
}

void permute(char arr[], int start, int end)
{
    int i=0;

    if (start == end)
    {
        printf("%s\n", arr);
        return;
    }

    for (i=start; i<end; i++)
    {
        swap(&arr[start], &arr[i]);
        permute(arr, start + 1, end);
        swap(&arr[start], &arr[i]);
    }
}


int main(void)
{
    char p[] = "abcdef";
    permute(p, 0, strlen(p));

    return 0;
}

